package com.wenge.datagroup.storage.dataprocess;

import com.wenge.datagroup.storage.util.ConfigUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @Author：WeiPengCheng
 * @Date：2022-07-27 10:55
 * @desc:多语言关键词抽取
 **/
public class MultilingualKeywords {
    private static Logger logger = LoggerFactory.getLogger(MultilingualKeywords.class);
    private static String url;
//    private static final String url = "https://tilake.wenge.com/gateway/b2_key_mord_extract/nlp/keywordExt";

    public static void setUrl(String url) {
        MultilingualKeywords.url = url;
    }

    public static void main(String[] args) {
        ConfigUtil.init();
        PropertyConfigurator.configure("config/log4j.properties");
/*
        String content =
                "{\"title\":\"黄瓜拌面做法 怎样做黄瓜拌面\",\"content\":\"导读文章来源:伊秀黄瓜适量，挂面一把，香菜适量，熟花生适量，辣椒粉一勺，蒜末一勺，葱花一勺，白芝麻一勺，白糖一勺，生抽三勺，陈醋两勺... " +
                        "黄瓜适量，挂面一把，香菜适量，熟花生适量，辣椒粉一勺，蒜末一勺，葱花一勺，白芝麻一勺，白糖一勺，生抽三勺，陈醋两勺，花椒粉半勺，盐适量。先准备出一个干净的碗，碗中放入一勺辣椒粉，一瓣剁碎的蒜末，一勺切碎的葱花，继续加入一勺熟花生，半勺花椒粉，一勺白芝麻，接着淋上热油搅拌一下，放入三勺生抽，两勺陈醋，一勺白糖，少许食盐酱汁就调好了。锅中烧水，水开后，拿出一把挂面放入锅中，煮上三分钟左右，煮好后捞出面条放到凉水中备用。把香菜切碎，黄瓜切丝，放凉的面条放入之前准备好的酱汁中，把香菜黄瓜丝放到面条上面，最后放入几颗熟花生，搅拌均匀就可以吃了。开胃爽口最适合大夏天吃了。郑重声明：本文版权归原作者所有，转载文章仅为传播更多信息之目的，如作者信息标记有误，请第一时候联系我们修改或删除，多谢\",\"topn\":\"5\",\"language\":\"中文\"}\n";
*/
        String content = "{\n" +
                "\"title\":\"A dynamic bayesian network click model for web search ranking\",\n" +
                "\"content\":\"As with any application of machine learning , web search ranking requires labeled data . The labels usually come in "
                +
                "the form of relevance assessments made by editors . Click logs can also provide an important source of implicit feedback and can "
                +
                "be used as a cheap proxy for editorial labels . The main difficulty however comes from the so called position bias - urls "
                +
                "appearing in lower positions are less likely to be clicked even if they are relevant . In this paper , we propose a Dynamic "
                +
                "Bayesian Network which aims at providing us with unbiased estimation of the relevance from the click logs . Experiments show that "
                +
                "the proposed click model outperforms other existing click models in predicting both click-through rate and relevance .\",\n"
                +
                "\"topn\":5, \n" +
                "\"language\":\"英语\"\n" +
                "}";


//        analysis(content);

        String con =
                "七月十日早晨八点三十左右，在临沂市人民医院突然进来五辆警车（其中一辆是法院的车），前前后后出来二、三十警察，大部份身穿便衣，这些人强行威逼李长芳家属签字出院。家人拒绝签字，身穿便衣的警察开始动手抓人打人，企图以暴力威胁，强迫签字。李长芳的儿子王小飞上厕所时，被便衣警察暴打。李长芳儿子挣脱后唿救：警察打人了！把我妈妈迫害得昏迷不醒，还打我，想把我也抓进去。我妈妈修炼法轮功做好人，强身健体没有错……警察去年十月翻墙绑架我妈妈到看守所，非法判刑，突然告诉我们我妈妈身体不行了，要住院开刀，问医生说是阑尾炎，后来开刀后被送进了重症监护室，开刀位置是从胸腔往下开刀的；再次问，医生说是多器官衰竭，至今昏迷不醒。现在人还没给我们治好，又强行逼我们出院。医院看病的人纷纷拥上来，有人拿出手机拍照。";
        String title = "";


        analysis(title, con, "中文");

    }

    public static JSONArray analysis(String title, String content, String language) {
        long start = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("title", title);
        jsonObject.put("content", content);
        jsonObject.put("topn", 10);
        jsonObject.put("language", language);


        OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("id", "1")
                .addFormDataPart("content", jsonObject.toJSONString())
                .addFormDataPart("callback", "http://1.com")
                .build();

        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();

        try {
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200) {
                logger.info("多语种关键词抽取成功!!");
                String data = response.body().string();
                if (JSON.parseObject(data).getString("code").equals("200")) {
                    JSONArray jsonArray = JSON
                            .parseObject(data)
                            .getJSONObject("data")
                            .getJSONArray("keywords");
                    logger.info("多语种关键词结果:" + jsonArray.toJSONString());
                    return jsonArray;
                }
            } else {
                logger.info("多语种关键词接口请求异常! code:{}", response.code());
            }
        } catch (IOException e) {
            logger.error("多语种关键词接口请求异常!");
            e.printStackTrace();
        } finally {
            logger.info("多语言关键词抽取接口处理耗时： " + (System.currentTimeMillis() - start) + " ms");
        }

        return new JSONArray();
    }
}
